<?php
namespace Index\Controller;
use think\Controller;
use think\Session;
class IndexController extends Controller{



	public function index(){

		session('en',NULL);  //如果中文界面， 就清空 en session. 

		//web config
		$getInfo = M('web_config');
		$getConf = $getInfo->find(1);
		$getConf['logo_img'] = '/Public/Uploads/'.$getConf['logo_img'];
		
	

		//get category
		$cate = M('category');
		$getTopCate = $cate->where(['pid'=>0])->order('sort asc')->select();

		foreach($getTopCate as &$v){
			$v['sub'] = $cate->where(['pid'=>$v['id']])->select();
		}

		//get product
		$goods = M('products');

		// 页码信息处理
		$pageSize 	= I('post.pageSize') ? I('post.pageSize') : 10;   //页尺寸
		$pageNo 	= I('post.pageNo') ? I('post.pageNo') : 1;		//页码
		
		$limit = '20';   //20个产品展示首页
		$getPro = $goods->field('id,name,product_img')->where('is_recomemd=1')->select();
		//$getcount = $goods->field('count(*) as count')->find();  //总记录数
		//echo $goods->_sql();die;
		$countpage = ceil($getcount['count']/$pageSize);   //数数总共有多少页

		/*页码信息拼装数组*/
		$count=[];  //初始化数组
		$count['pageSize'] = $pageSize;
		$count['pageNo'] = $pageNo;
		$count['count'] = $getcount['count'];
		$count['countpage'] = $countpage;


		
		foreach($getPro as &$v){  //拼装信息
			$v['product_img'] = '/Public/product_img'.$v['product_img'];
		}


		//选4个最新产品 放到首页右侧展示
		$getNewProducts = $goods->field('id,product_img')->order('id desc')->limit(4)->select();
		foreach($getNewProducts as &$v1){
			$v1['product_img'] = '/Public/product_img'.$v1['product_img'];
		}

		//get company info 
		$cmp = M('company_info');
		$getCmpInfo = $cmp->find(1);
		$getCmpInfo['brief'] = html_entity_decode($getCmpInfo['brief']);  //html_entity_decode 该函数将 html 转化， 就不会显示html 代码
		
		

	
		$this->assign([
			'getConf'  =>  $getConf, 
			'getCate' => $getTopCate, 
			'getPro'  => $getPro,
			'getCmpInfo' => $getCmpInfo,
			'getNewProducts' =>$getNewProducts,
			'count'  => $count,

		]);
		$this->display();
	}


	//英文版
	public function en_index(){

		session('en',1);  //设置 en session, 用作判断产品等信息 输出中文还是英文

		//web config
		$getInfo = M('web_config');
		$getConf = $getInfo->find(1);
		$getConf['logo_img'] = '/Public/Uploads/'.$getConf['logo_img'];
		
	

		//get category
		$cate = M('category');
		$getTopCate = $cate->where(['pid'=>0])->order('sort asc')->select();

		foreach($getTopCate as &$v){
			$v['sub'] = $cate->where(['pid'=>$v['id']])->select();
		}

		//get product
		$goods = M('products');

		
		$limit = '20';   //20个产品展示首页
		$getPro = $goods->field('id,ename,product_img')->limit($limit)->select();
	
		foreach($getPro as &$v){  //拼装信息
			$v['product_img'] = '/Public/product_img'.$v['product_img'];
		}


		//选4个最新产品 放到首页右侧展示
		$getNewProducts = $goods->field('id,product_img')->order('id desc')->limit(4)->select();
		foreach($getNewProducts as &$v1){
			$v1['product_img'] = '/Public/product_img'.$v1['product_img'];
		}

		//get company info 
		$cmp = M('company_info');
		$getCmpInfo = $cmp->find(1);
		$getCmpInfo['enbrief'] = html_entity_decode($getCmpInfo['enbrief']);  //html_entity_decode 该函数将 html 转化， 就不会显示html 代码
		
		

	
		$this->assign([
			'getConf'  =>  $getConf, 
			'getCate' => $getTopCate, 
			'getPro'  => $getPro,
			'getCmpInfo' => $getCmpInfo,
			'getNewProducts' =>$getNewProducts,
			'count'  => $count,

		]);
		$this->display();
	}




	
	


}
